package cn.itsource.hrm.service.impl;

import cn.itsource.feign.client.RedisFeignClient;
import cn.itsource.hrm.domain.Sso;
import cn.itsource.hrm.domain.VipBase;
import cn.itsource.hrm.dto.RegisterDto;
import cn.itsource.hrm.mapper.SsoMapper;
import cn.itsource.hrm.mapper.VipBaseMapper;
import cn.itsource.hrm.service.ISsoService;
import cn.itsource.hrm.utils.AjaxResult;
import cn.itsource.hrm.utils.encrypt.MD5;
import cn.itsource.hrm.vo.SendSmsCodeVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author luo
 * @since 2020-04-29
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {

    @Autowired
    private RedisFeignClient redisFeignClient ;

    @Autowired
    private VipBaseMapper vipBaseMapper ;

    @Override
    public void register(RegisterDto registerDto) {
        //1.基本参数判断
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        String smsCode = registerDto.getSmsCode();
        if(StringUtils.isBlank(mobile) || StringUtils.isBlank(password) || StringUtils.isBlank(smsCode)){
            throw new RuntimeException("参数无效");
        }
        //判断手机号是否已经被注册
        Sso ssoInMysql = baseMapper.selectByPhone(mobile);
        if(ssoInMysql != null){
            throw new RuntimeException("手机号已经被注册");
        }

        //2.判断手机验证码，从Redis获取手机验证码
        String smsCodeKey ="REGISTER:SMS:"+mobile;
        AjaxResult ajaxResult = redisFeignClient.get(smsCodeKey);
        if(!ajaxResult.isSuccess() || ajaxResult.getResultObj() == null){
            throw new RuntimeException("验证码获取失败，请重新发送");
        }
        //从Redis获取手机验证码
        SendSmsCodeVo sendSmsCodeVo = JSON.parseObject(ajaxResult.getResultObj().toString(), SendSmsCodeVo.class);
        if(!sendSmsCodeVo.getSmsCode().equals(smsCode)){
            throw new RuntimeException("手机验证码错误，请重新发送");
        }

        //3.保存数据到t_sso
        Sso sso = new Sso();
        sso.setPhone(mobile);

        //随机盐
        String salt = MD5.getRandomCode(16);
        //密码加密
        String md5Pass = MD5.getMD5(password + salt);
        sso.setPassword(md5Pass);
        sso.setSalt(salt);
        Long nowTime = new Date().getTime();
        sso.setCreateTime(nowTime);
        baseMapper.insert(sso);

        //4.保存关联表：t_vip_base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(nowTime);
        vipBase.setSsoId(sso.getId());
        vipBase.setRegChannel(1);
        vipBase.setRegTime(nowTime);
        vipBaseMapper.insert(vipBase);
    }

    @Override
    public String login(Sso sso) {
        //1.判断参数 判断是否输入了有效的账号密码
        if(StringUtils.isBlank(sso.getPhone())||StringUtils.isBlank(sso.getPassword())){
            throw new RuntimeException("用户名和密码不能为空");
        }
        //2.判断这个用户存不存在 数据库有没有这个手机号
        Sso sso1 = baseMapper.selectByPhone(sso.getPhone());
        if(sso1 == null){
            throw new RuntimeException("此用户不存在");
        }
        //3.存在后 判断密码是否正确
        //因为数据库的是密文所以   把前台得到的密码明文变密文
        String md5Pass = MD5.getMD5(sso.getPassword() + sso1.getSalt());
        if(!sso1.getPassword().equals(md5Pass)){
            throw new RuntimeException("密码错误");
        }
        //4.生成Redis 的Key
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //5.把登录信息放到Redis
        AjaxResult ajaxResult = redisFeignClient.setex(token, 24 * 60 * 60, JSON.toJSONString(sso1));
        //如果找不到
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("登录信息保存失败，请重新登录");
        }
        System.out.println(token);
        return token;
    }
}
